﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using KeRui.Model;
using System.Text;
using System.Collections.Generic;

public partial class Manage_Cms_CatchParseItem : System.Web.UI.Page
{
    protected KeRui.BLL.CmsCatchParse bllCmsCatchParse = new KeRui.BLL.CmsCatchParse();
    protected KeRui.Model.CmsCatchParse modelCmsCatchParse = new CmsCatchParse();
    protected KeRui.BLL.CmsCatch bllCmsCatch = new KeRui.BLL.CmsCatch();
    public string Act = ManageHelper.PageAct();
    private ManageErrMsg nsb = new ManageErrMsg();
    protected string ActFlag = "addsave";
    protected string ActName = "添加";
    protected KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();

    private KeRui.BLL.CmsCatchParseRegex bllRegx = new KeRui.BLL.CmsCatchParseRegex();
    private KeRui.Model.CmsCatchParseRegex modelRegx = new KeRui.Model.CmsCatchParseRegex();
    private List<KeRui.Model.CmsCatchParseRegex> list = new List<CmsCatchParseRegex>();


    protected void Page_Load(object sender, EventArgs e)
    {
        //登录
        ManageHelper.CheckAdminLogin();
        //权限
        ManageHelper.CheckAdminPower("system_catchparsemanage");
        switch (ManageHelper.PageAct())
        {
            case "addsave":
                Add();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            //----集解析内容调整
            case "regadd":
                RegAdd();
                break;
            case "regaddsave":
                RegAddSave();
                break;
            case "regedit":
                RegEdit();
                break;
            case "regeditsave":
                RegEditSave();
                break;
            case "regdel":
                RegDel();
                break;
            default:
                break;
        }

    }
    #region 解析管理

    #region 添加
    private void Add()
    {
        ManageHelper.CheckAdminPower("system_catchparesmanageadd");
        modelCmsCatchParse.ParseName = PageRequest.GetString("ParseName");
        if (modelCmsCatchParse.ParseName == "")
        {
            nsb.AddErr("请输入名称。");
        }

        modelCmsCatchParse.ListEncoding = PageRequest.GetString("ListEncoding");
        modelCmsCatchParse.ContentEncoding = PageRequest.GetString("ContentEncoding");

        #region //列表


        modelCmsCatchParse.ListParseBegin = PageRequest.GetString("ListParseBegin");
        if (modelCmsCatchParse.ListParseBegin == "")
        {
            nsb.AddErr("请输入定位开头。");
        }
        modelCmsCatchParse.ListParseEnd = PageRequest.GetString("ListParseEnd");
        if (modelCmsCatchParse.ListParseEnd == "")
        {
            nsb.AddErr("请输入定位结尾。");
        }
        modelCmsCatchParse.ListParseTags = PageRequest.GetString("ListParseTags");
        if (modelCmsCatchParse.ListParseTags == "")
        {
            nsb.AddErr("请输入ListParseTags。");
        }
        modelCmsCatchParse.ListParseAttrs = PageRequest.GetString("ListParseAttrs");
        if (modelCmsCatchParse.ListParseAttrs == "")
        {
            nsb.AddErr("请输入ListParseAttrs。");
        }
        modelCmsCatchParse.ListParseRule = PageRequest.GetString("ListParseRule");
        if (modelCmsCatchParse.ListParseRule == "")
        {
            nsb.AddErr("请输入ListParseRule。");
        }
        #endregion

        #region //内容

        modelCmsCatchParse.ContentUrl = PageRequest.GetString("ContentUrl");
        if (modelCmsCatchParse.ContentUrl == "")
        {
            nsb.AddErr("请输入ContentUrl。");
        }
        modelCmsCatchParse.ContentParseBegin = PageRequest.GetString("ContentParseBegin");
        if (modelCmsCatchParse.ContentParseBegin == "")
        {
            nsb.AddErr("请输入ContentParseBegin。");
        }
        modelCmsCatchParse.ContentParseEnd = PageRequest.GetString("ContentParseEnd");
        if (modelCmsCatchParse.ContentParseEnd == "")
        {
            nsb.AddErr("请输入ContentParseEnd。");
        }
        modelCmsCatchParse.ContentParseTags = PageRequest.GetString("ContentParseTags");
        if (modelCmsCatchParse.ContentParseTags == "")
        {
            nsb.AddErr("请输入ContentParseTags。");
        }
        modelCmsCatchParse.ContentParseAttrs = PageRequest.GetString("ContentParseAttrs");
        if (modelCmsCatchParse.ContentParseAttrs == "")
        {
            nsb.AddErr("请输入ContentParseAttrs。");
        }
        modelCmsCatchParse.ContentParseRule = PageRequest.GetString("ContentParseRule");
        if (modelCmsCatchParse.ContentParseRule == "")
        {
            nsb.AddErr("请输入ContentParseRule。");
        }
        modelCmsCatchParse.ContentPageParseRule = PageRequest.GetString("ContentPageParseRule");
        #endregion

        nsb.ChkErr();
        int Identtity = bllCmsCatchParse.Add(modelCmsCatchParse);
        if (Identtity < 0)
        {
            nsb.AddErr("已有相同的模型名称。");
        }
        else
        {
            //日志
            ManageHelper.AddLog("system_catchparesmanageadd", "添加了采集解析：" + modelCmsCatchParse.ParseName);
            Response.Redirect("catchParselist.aspx?sucmsg=添加成功。");
        }
    }
    #endregion

    #region 反显修改页面
    private void Edit()
    {

        ActFlag = "editsave";
        ActName = "修改";
        int CatchID = PageRequest.GetInt("ID", -1);
        if (CatchID <= 0)
        {
            nsb.AddErr("非法操作。");
            nsb.ChkErr();
        }
        modelCmsCatchParse = bllCmsCatchParse.GetModel(CatchID);
        if (modelCmsCatchParse == null)
        {
            nsb.AddErr("该采集解析不存在。");
            nsb.ChkErr();
        }
    }
    #endregion

    #region 修改时保存
    private void EditSave()
    {
        ManageHelper.CheckAdminPower("system_catchparesmanageupdate");
        modelCmsCatchParse.ParseID = PageRequest.GetInt("ParseID", -1);
        modelCmsCatchParse.ParseName = PageRequest.GetString("ParseName");
        if (modelCmsCatchParse.ParseName == "")
        {
            nsb.AddErr("请输入名称。");
        }

        modelCmsCatchParse.ListEncoding = PageRequest.GetString("ListEncoding");
        modelCmsCatchParse.ContentEncoding = PageRequest.GetString("ContentEncoding");

        #region //列表
        modelCmsCatchParse.ListParseBegin = PageRequest.GetString("ListParseBegin");
        if (modelCmsCatchParse.ListParseBegin == "")
        {
            nsb.AddErr("请输入定位开头。");
        }
        modelCmsCatchParse.ListParseEnd = PageRequest.GetString("ListParseEnd");
        if (modelCmsCatchParse.ListParseEnd == "")
        {
            nsb.AddErr("请输入定位结尾。");
        }
        modelCmsCatchParse.ListParseTags = PageRequest.GetString("ListParseTags");
        if (modelCmsCatchParse.ListParseTags == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        modelCmsCatchParse.ListParseAttrs = PageRequest.GetString("ListParseAttrs");
        if (modelCmsCatchParse.ListParseAttrs == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        modelCmsCatchParse.ListParseRule = PageRequest.GetString("ListParseRule");
        if (modelCmsCatchParse.ListParseRule == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        #endregion

        #region //内容
        modelCmsCatchParse.ContentUrl = PageRequest.GetString("ContentUrl");
        if (modelCmsCatchParse.ContentUrl == "")
        {
            nsb.AddErr("请输入内容目标地址。");
        }
        modelCmsCatchParse.ContentParseBegin = PageRequest.GetString("ContentParseBegin");
        if (modelCmsCatchParse.ContentParseBegin == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        modelCmsCatchParse.ContentParseEnd = PageRequest.GetString("ContentParseEnd");
        if (modelCmsCatchParse.ContentParseEnd == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        modelCmsCatchParse.ContentParseTags = PageRequest.GetString("ContentParseTags");
        if (modelCmsCatchParse.ContentParseTags == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        modelCmsCatchParse.ContentParseAttrs = PageRequest.GetString("ContentParseAttrs");
        if (modelCmsCatchParse.ContentParseAttrs == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        modelCmsCatchParse.ContentParseRule = PageRequest.GetString("ContentParseRule");
        if (modelCmsCatchParse.ContentParseRule == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        modelCmsCatchParse.ContentPageParseRule = PageRequest.GetString("ContentPageParseRule");
        #endregion

        nsb.ChkErr();
        int RetFlag = bllCmsCatchParse.Update(modelCmsCatchParse);
        if (RetFlag < 0)
        {
            nsb.AddErr("对不起！已存在相同的内容模型。");
            nsb.ChkErr();
        }
        else
        {
            ManageHelper.AddLog("system_catchparesmanageupdate", "修改了采集解析：" + modelCmsCatchParse.ParseName);
            Response.Redirect("catchParselist.aspx?sucmsg=修改成功。");
        }
    }
    #endregion

    #region 删除
    private void Del()
    {
        ManageHelper.CheckAdminPower("system_catchparesmanagedelete");
        int ModelID = PageRequest.GetInt("ID", -1);
        if (ModelID <= 0)
        {
            nsb.AddErr("该站点不存在。");
            nsb.ChkErr();
        }
        bllCmsCatchParse.Delete(ModelID);
        ManageHelper.AddLog("system_catchparesmanagedelete", "删除了采集解析，ID：" + ModelID);
        Response.Redirect("catchParselist.aspx?sucmsg=恭喜。删除成功。");
    }
    #endregion

    #endregion

    #region 集解析内容调整——ajax

    #region 组建添加页面
    private void RegAdd()
    {
        int ParseID = PageRequest.GetInt("ParseID", -1);
        modelCmsCatchParse = bllCmsCatchParse.GetModel(ParseID);
        if (modelCmsCatchParse == null)
        {

            Response.Write("ddd" + ParseID); Response.End();
        }
        StringBuilder RetMsg = new StringBuilder();
        RetMsg.Append("<div class=\"columntitle\">解析内容</div><form id=\"myfrm\">" +
        "<table width=\"90%\">" +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"\" onclick=\"helpInf(this)\" /> 字段名称：</td>" +
                "<td align=\"left\">" +
                    "<select name=\"FieldName\" id=\"FieldName\">" +
                    GetSelect(modelCmsCatchParse.ContentParseRule, "") +
                    "</select>" +
                "</td>" +
            "</tr> " +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"匹配正则表达式模式\" onclick=\"helpInf(this)\" /> 匹配正则表达式模式：</td>" +
                "<td align=\"left\">" +
                    "<textarea  name=\"Pattern\" ID=\"Pattern\" valid=\"required\" errmsg=\"请输入匹配正则表达式模式\"style=\"width:300px;height:60px;\" class=\"colorblur\"  >" + modelRegx.Pattern + "</textarea>" +
                "</td>" +
            "</tr>" +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"替换字符串\" onclick=\"helpInf(this)\" /> 替换字符串：</td>" +
                "<td align=\"left\">" +
                    "<textarea  name=\"Replacement\" ID=\"Replacement\" style=\"width:300px;height:60px;\" class=\"colorblur\"  >" + modelRegx.Replacement + "</textarea>" +
                "</td>" +
            "</tr>  " +
             "<tr>" +
                "<td colspan=\"2\" align=\"center\" class=\"tdbutton\">" +
                "<input type=\"button\" name=\"btnAdd\" value=\"保 存\" onclick=\"if(validator($$('myfrm'))){RegAddSave(" + modelCmsCatchParse.ParseID + ")}\"/> " +
                "<input type=\"button\" name=\"Cancel\" value=\"关 闭\" onclick=\"javascript:document.getElementById('RegDiv').style.display='none';\"/>" +
                "<input type=\"hidden\" value=\"" + modelCmsCatchParse.ParseID + "\" name=\"ParseID\" ID=\"ParseID\" />" +
                "</td>" +
            "</tr>" +
        "</table></from>");
        Response.Write(RetMsg.ToString());
        Response.End();
    }
    #endregion

    #region 添加时保存
    private void RegAddSave()
    {
        string RetStr = "";
        modelRegx.ParseID = PageRequest.GetInt("ParseID", -1);
        if (modelRegx.ParseID == -1)
        {
            Response.Write("1"); Response.End();
        }
        modelRegx.FieldName = PageRequest.GetString("FieldName");
        if (modelRegx.FieldName == "")
        {
            Response.Write("2"); Response.End();
        }
        modelRegx.Pattern = PageRequest.GetString("Pattern");
        if (modelRegx.Pattern == "")
        {
            Response.Write("3"); Response.End();
        }
        modelRegx.Replacement = PageRequest.GetString("Replacement");
        //if (modelRegx.Replacement == "")
        //{
        //    Response.Write("4"); Response.End();
        //}
        bllRegx.Add(modelRegx);
        RetStr = BoundData(modelRegx.ParseID);
        Response.Write(RetStr); Response.End();
    }
    #endregion

    #region 组建编辑页面
    private void RegEdit()
    {

        StringBuilder RetMsg = new StringBuilder();
        int ID = PageRequest.GetInt("ID", -1);
        modelRegx = bllRegx.GetModel(ID);
        if (modelRegx == null)
        {
            Response.Write("error"); Response.End();
        }
        modelCmsCatchParse = bllCmsCatchParse.GetModel(modelRegx.ParseID);
        if (modelCmsCatchParse == null)
        {
            Response.Write("error" + modelRegx.ParseID); Response.End();
        }
        RetMsg.Append("<div class=\"columntitle\">解析内容</div><form id=\"myfrm\">" +
        "<table width=\"90%\">" +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"\" onclick=\"helpInf(this)\" /> 字段名称：</td>" +
                "<td align=\"left\">" +
                    "<select name=\"FieldName\" id=\"FieldName\">" +
                    GetSelect(modelCmsCatchParse.ContentParseRule, modelRegx.FieldName) +
                    "</select>" +
                "</td>" +
            "</tr> " +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"匹配正则表达式模式\" onclick=\"helpInf(this)\" /> 匹配正则表达式模式：</td>" +
                "<td align=\"left\">" +
                    "<textarea  name=\"Pattern\" ID=\"Pattern\" valid=\"required\" errmsg=\"请输入匹配正则表达式模式\"style=\"width:300px;height:60px;\" class=\"colorblur\"  >" + modelRegx.Pattern + "</textarea>" +
                "</td>" +
            "</tr>" +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"替换字符串\" onclick=\"helpInf(this)\" /> 替换字符串：</td>" +
                "<td align=\"left\">" +
                    "<textarea  name=\"Replacement\" ID=\"Replacement\" style=\"width:300px;height:60px;\" class=\"colorblur\"  >" + modelRegx.Replacement + "</textarea>" +
                "</td>" +
            "</tr>  " +
             "<tr>" +
                "<td colspan=\"2\" align=\"center\" class=\"tdbutton\">" +
                "<input type=\"button\" name=\"btnAdd\" value=\"保 存\" onclick=\"if(validator($$('myfrm'))){RegEditSave(" + modelRegx.ID + ")}\"/> " +
                "<input type=\"button\" name=\"Cancel\" value=\"关 闭\" onclick=\"javascript:document.getElementById('RegDiv').style.display='none';\"/>" +
                "<input type=\"hidden\" value=\"" + modelCmsCatchParse.ParseID + "\" name=\"ParseID\" ID=\"ParseID\" />" +
                "</td>" +
            "</tr>" +
        "</table></from>");
        Response.Write(RetMsg.ToString());
        Response.End();
    }
    #endregion

    #region 编辑时保存
    private void RegEditSave()
    {
        modelRegx.ID = PageRequest.GetInt("ID", -1);
        KeRui.Model.CmsCatchParseRegex oldmodel = new CmsCatchParseRegex();
        oldmodel = bllRegx.GetModel(modelRegx.ID);
        if (oldmodel == null)
        {
            Response.Write("error"); Response.End();
        }
        string RetStr = "";
        modelRegx.FieldName = PageRequest.GetString("FieldName");
        if (modelRegx.FieldName == "")
        {
            Response.Write("2"); Response.End();
        }
        modelRegx.Pattern = PageRequest.GetString("Pattern");
        if (modelRegx.Pattern == "")
        {
            Response.Write("3"); Response.End();
        }
        modelRegx.Replacement = PageRequest.GetString("Replacement");
        //if (modelRegx.Replacement == "")
        //{
        //    Response.Write("4"); Response.End();
        //}
        bllRegx.Update(modelRegx);
        RetStr = BoundData(oldmodel.ParseID);
        Response.Write(RetStr); Response.End();
    }
    #endregion

    #region 删除
    private void RegDel()
    {
        int ID = PageRequest.GetInt("ID", -1);
        modelRegx = bllRegx.GetModel(ID);
        if (modelRegx == null)
        {
            Response.Write("信息不存在。"); Response.End();
        }
        else
        {
            bllRegx.Delete(ID);
            Response.Write(BoundData(modelRegx.ParseID)); Response.End();
        }
    }
    #endregion

    #region 绑定数据
    protected string BoundData(int ParseID)
    {
        StringBuilder RetData = new StringBuilder();
        RetData.Append("<table cellspacing=\"0\" cellpadding=\"0\" rules=\"all\"  border=\"0\" id=\"MyDataGrid\" style=\"border-width: 0px; width: 100%; border-collapse: collapse;\">");
        RetData.Append("<tr class=\"summary-title\" style=\"height: 25px;\">" +
                    "<td>&nbsp;字段名称</td>" +
                    "<td>匹配正则表达式模式</td>" +
                    "<td>替换字符串</td>" +
                    "<td>创建时间</td>" +
                    "<td>操作</td>" +
                "</tr>");

        list = bllRegx.GetList(ParseID);
        foreach (KeRui.Model.CmsCatchParseRegex model in list)
        {
            RetData.Append("<tr class=\"tdbg\" onmouseover=\"this.className='tdbg-dark';\" onmouseout=\"this.className='tdbg';\" style=\"height:25px;\">" +
                        "<td>&nbsp;" + model.FieldName + "</td>" +
                        "<td>" + HttpUtility.HtmlEncode(model.Pattern) + "</td>" +
                        "<td>" + model.Replacement + "</td>" +
                        "<td>" + model.CreateTime + "</td>" +
                        "<td><a href=\"javascript:RegEditPage(" + model.ID + ")\">修改<a> | <a href=\"javascript:if(confirm('确定要删除。')){RegDel(" + model.ID + ")}\">删除</a></td>" +
                    "</tr>");
        }
        RetData.Append("</table>");
        return RetData.ToString();
    }
    #endregion

    private string GetSelect(string strRegx, string strTep)
    {
        string retStr = "";
        string[] arr = bllRegx.GetRegexFiledArray(strRegx);
        for (int i = 0; i < arr.Length; i++)
        {
            string strSel = "";
            if (strTep == arr[i])
            {
                strSel = "selected";
            }
            retStr += "<option value=" + arr[i] + " " + strSel + ">" + arr[i] + "</option>";
        }
        return retStr;
    }
    #endregion
}
